<?php

class Model_Institution extends Model_Pandrade {

    const TABLE = "institution";

    /**
     * @var protected $id id value
     * @method int get_id() get_id(void)
     * @method void set_id() set_id(int $id)
     */
    protected $id;

    /**
     * @var protected $university_name university_name value
     * @method string get_university_name() get_university_name(void)
     * @method void set_university_name() set_university_name(string $university_name)
     */
    protected $university_name;

    /**
     * @var protected $institute_name institute_name value
     * @method string get_institute_name() get_institute_name(void)
     * @method void set_institute_name() set_institute_name(string $institute_name)
     */
    protected $institute_name;

    /**
     * @var protected $city city value
     * @method string get_city() get_city(void)
     * @method void set_city() set_city(string $city)
     */
    protected $city;

    /**
     * @var protected $state state value
     * @method string get_state() get_state(void)
     * @method void set_state() set_state(string $state)
     */
    protected $state;

    /**
     * @var protected $user_id user_id value
     * @method int get_user_id() get_user_id(void)
     * @method void set_user_id() set_user_id(int $user_id)
     */
    protected $user_id;

    /**
     * @return Model_Institution
     */
    public function __construct($id = 0) {
        if (0 < intval($id)) {
            $db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
            $db = $db->as_assoc()->execute();
            if ($db->count() > 0) {
                $this->load($db->current());
            }
        }
    }

    protected static function get_database_fields() {
        return array(
            'university_name' => __("Institution_University_name"),
            'institute_name' => __("Institution_Institute_name"),
            'city' => __("Institution_City"),
            'state' => __("Institution_State"),
            'user_id' => __("Institution_User_id"),
        );
    }

    public function validate() {
        $validateArray = array();
        foreach (self::get_database_fields() as $key => $value) {
            $validateArray[$key] = $this->get($key);
        }
        $validation = Validation::factory($validateArray)
                ->rule('university_name', 'not_empty')
                ->rule('institute_name', 'not_empty')
                ->rule('city', 'not_empty')
                ->rule('state', 'not_empty')
                ->rule('user_id', 'not_empty')
        ;
        if ($validation->check()) {
            return $validateArray;
        } else {
            throw new Validation_Exception('validation', $validation);
        }
    }

    public static function get_report() {
        return DB::select(
                                "id"
                                , "university_name"
                                , "institute_name"
                                , "city"
                                , "state"
                                , "user_id"
                                , array("id", "actions"))
                        ->from(self::TABLE)
                        ->order_by("id", "ASC");
    }

    public static function get_user_institution($user_id, $only_id = true) {
        $id = DB::select("institution_id")->from("user_institution")
                        ->where("user_id", "=", $user_id)
                        ->as_object()->execute();
        if ($only_id) {
            return $id->get("id");
        } else {
            if ($id->count() > 0) {
                return new Model_Institution($id->get("id"));
            } else {
                return 0;
            }
        }
    }

    public static function search_institution($institution_expr = NULL, $institution_city_id = NULL) {
        if ($institution_expr === NULL || $institution_city_id === NULL) {
            die("aqui");
            return array();
        } else if ($institution_expr == "") {
            $institutions = DB::select(self::TABLE . ".id", self::TABLE . ".name", self::TABLE . ".acronym")->from(self::TABLE)
                    ->join(Model_City::TABLE)
                    ->on(self::TABLE . ".city", "=", Model_City::TABLE . ".id");
            if ($institution_city_id != "") {
                $institutions->where(self::TABLE . ".city", "=", $institution_city_id);
            }
            $institutions->limit(15)->order_by(self::TABLE . ".name", "ASC");
//            die(print_r(array("$institutions")));
            return $institutions->execute()->as_array();
        } else {
            //expressions with more than word be like: "%WORD1%WORD2%" (for mysql)
            $words = explode(" ", $institution_expr);
            $size = sizeof($words);
            $i = $size;
            $mysql_pattern = "";
            do {
                $mysql_pattern .= $words[$size - $i];
                $mysql_pattern .= "%";
                $i--;
            } while ($i > 0);
            $institutions = DB::select(self::TABLE . ".id", self::TABLE . ".name", self::TABLE . ".acronym")
                    ->from(self::TABLE)
                    ->join(Model_City::TABLE)
                    ->on(self::TABLE . ".city", "=", Model_City::TABLE . ".id")
                    ->where_open()
                    ->where(self::TABLE . ".name", "LIKE", $mysql_pattern)
                    ->or_where(self::TABLE . ".acronym", "LIKE", $mysql_pattern)
                    ->where_close();
            if ($institution_city_id != "") {
                $institutions->and_where(self::TABLE . ".city", "=", $institution_city_id);
            }
            $institutions->limit(15)->order_by(self::TABLE . ".name", "ASC"); //->execute()->as_array();
            return $institutions->execute()->as_array();
        }
    }
    public static function get_institution_from($institution_city_id){
        return DB::select("id")->from(self::TABLE)->where("city","=",$institution_city_id)
                ->execute()->as_array();
    }

}
